/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jess.arms.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jess.arms.R;
import com.jess.arms.base.delegate.IFragment;
import com.jess.arms.callback.CustomCallback;
import com.jess.arms.callback.EmptyCallback;
import com.jess.arms.callback.ErrorCallback;
import com.jess.arms.callback.LoadSirManagement;
import com.jess.arms.callback.LottieLoadingCallback;
import com.jess.arms.callback.TimeoutCallback;
import com.jess.arms.integration.cache.Cache;
import com.jess.arms.integration.cache.CacheType;
import com.jess.arms.integration.lifecycle.FragmentLifecycleable;
import com.jess.arms.mvp.IPresenter;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.jess.arms.widget.LoadingView;
import com.jess.arms.widget.onchangeview.OnChangeLayoutView;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.trello.rxlifecycle2.android.FragmentEvent;

import javax.inject.Inject;

import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * author : Alex
 * e-mail : xys9786@outlook.com
 * date   : 2020/4/1220:36
 * desc   :
 * version: 1.0
 */
public abstract class BaseNoLoadFragment<P extends IPresenter> extends Fragment implements IFragment, FragmentLifecycleable, IView {

    protected final String TAG = this.getClass().getSimpleName();
    private final BehaviorSubject<FragmentEvent> mLifecycleSubject = BehaviorSubject.create();
    protected Context mContext;
    @Inject
    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null
    private Cache<String, Object> mCache;
    private LoadService mBaseLoadService;
    private LoadingView loadingView;
    private OnChangeLayoutView mOnChangeLayoutView;

    @NonNull
    @Override
    public synchronized Cache<String, Object> provideCache() {
        if (mCache == null) {
            //noinspection unchecked
            mCache = ArmsUtils.obtainAppComponentFromContext(getActivity()).cacheFactory().build(CacheType.FRAGMENT_CACHE);
        }
        return mCache;
    }

    @NonNull
    @Override
    public final Subject<FragmentEvent> provideLifecycleSubject() {
        return mLifecycleSubject;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }
    protected abstract void onNetReload(View v);


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView(inflater, container, savedInstanceState);
    }

    protected abstract int onCreateFragmentView();

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = View.inflate(getActivity(), onCreateFragmentView(), null);
        return rootView;
    }

    /**
     * 销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();//释放资源
        }
        this.mPresenter = null;


        if (mBaseLoadService != null)
            this.mBaseLoadService = null;
    }

    /**
     * 解除
     */
    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    /**
     * 是否使用 EventBus
     */
    @Override
    public boolean useEventBus() {
        return true;
    }

    // 隐藏
    public void closeLoadingDialog() {
        if (loadingView != null)
            loadingView.dismiss();
    }

    //显示
    public void showLoadingDialog() {
        if (loadingView == null)
            loadingView = new LoadingView(getActivity(), R.style.CustomDialog);
        loadingView.show();

    }

    /**
     * 隐藏加载框
     */
    @Override
    public void hideLoading() {
        closeLoadingDialog();

    }

    /**
     * 显示加载框
     */
    @Override
    public void showLoading() {
        showLoadingDialog();

    }

    /**
     * 初始化 LoadSir
     */

    public void initLoadSir(Context context, View view) {
        mOnChangeLayoutView = new OnChangeLayoutView(context);
        mOnChangeLayoutView.setContentView(view);
        mOnChangeLayoutView.isShowTitle(false);
    }

    /**
     * 加载布局
     */
    @Override
    public void showLoadingLayout() {
        if (mOnChangeLayoutView!=null){
            mOnChangeLayoutView.showLoadingLayout();
        }

    }

    /**
     * 显示空白页面
     */
    @Override
    public void showEmpty() {
        if (mOnChangeLayoutView!=null)
            mOnChangeLayoutView.showEmpty();
    }

    /**
     * 显示内容
     */
    @Override
    public void showContent() {
        if (mOnChangeLayoutView!=null){
            mOnChangeLayoutView.showContent();
        }

    }

    /**
     * 显示加载失败
     */
    @Override
    public void showFailure() {
        if (mOnChangeLayoutView!=null){
            mOnChangeLayoutView.showFailure();
        }

    }

    /**
     * 显示无网络
     */
    @Override
    public void showNoNetwork() {
        if (mOnChangeLayoutView!=null){
            mOnChangeLayoutView.showNoNetwork();
        }
    }

    /**
     * 加载超时
     */
    @Override
    public void showTimeOut() {
        if (mOnChangeLayoutView!=null){
            mOnChangeLayoutView.showTimeOut();
        }
    }


    @Override
    public void setData(@Nullable Object data) {

    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
}
